package threadLearn.producerconsumer.v4;

import java.util.List;
import java.util.UUID;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

public class Producer implements Runnable {

	private List<String> basket;
	private ReentrantLock lock;
	private Condition condition;

	public Producer(List<String> basket, ReentrantLock lock, Condition condition) {
		this.basket = basket;
		this.lock = lock;
		this.condition = condition;
	}

	@Override
	public void run() {

		while (true) {
			lock.lock();
			try {
				while (basket.size() >= 6) {
					try {
						condition.await();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
				try {
					Thread.sleep(500);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				String str = UUID.randomUUID().toString();
				basket.add(str);
				System.out.println(Thread.currentThread() + " 生产了：" + str);
				condition.signalAll();
				System.out.println(Thread.currentThread()+" 通知大家消费了");
			} finally {
				//System.out.println(Thread.currentThread()+"释放了锁");
				lock.unlock();
			}
			

		}
	}

}
